import { createRouter, createWebHashHistory } from "vue-router";
// import { getRoutesList } from "@/request/api";
// // import HomeView from "@/views/HomeView.vue";
// // 首屏懒加载
// const HomeView = () => import("@/views/HomeView.vue");
// interface RouteMeta {
//   title: string;
// }

// interface RouteConfig {
//   name: string;
//   path: string;
//   menuId: number | string;
//   parentId: number | string;
//   meta: RouteMeta;
//   component: () => Promise<any>;
//   children?: RouteConfig[];
// }

// let allRoutes: any = [];

// 获取动态路由配置

// try {
//   const response = await getRoutesList();
//   function formatPath(path: string): string {
//     // 如果路径不以 '/' 开头，则在前面加上 '/'
//     if (!path.startsWith("/")) {
//       path = "/" + path;
//     }
//     return `../views${path}.vue`;
//   }
//   console.log(response.data.list);

//   allRoutes = response.data.list.map((item: RouteConfig) => ({
//     name: item.name,
//     menuId: item.menuId,
//     // 判断当路由出问题时
//     path: item.path.startsWith("/") ? item.path : "/" + item.path,
//     meta: item.meta,
//     component: HomeView,
//     children: item.children
//       ? item.children.map((child: any) => ({
//           parentId: child.parentId,
//           name: child.name,
//           path: child.path.startsWith("/") ? child.path : "/" + child.path,
//           meta: child.meta,
//           menuId: child.menuId,
//           component: () => import(/* @vite-ignore */ formatPath(child.path)),
//         }))
//       : [],
//   }));
//   console.log(allRoutes);
// } catch (error) {
//   console.error("获取路由列表失败:", error);
//   allRoutes = [
//     {
//       name: "HomeView",
//       path: "/HomeView",
//       meta: {
//         title: "首页",
//       },
//       component: () => import("@/views/HomeView.vue"),
//     },
//     {
//       name: "login",
//       path: "/",
//       meta: {
//         title: "登录页",
//       },
//       component: () => import("@/components/login/login.vue"),
//     },
//   ];
// }
let routes = [
  {
    name: "HomeView",
    path: "/HomeView",
    meta: {
      title: "首页",
    },
    component: () => import("@/views/HomeView.vue"),
  },
  {
    name: "insituationMonitoring",
    path: "/insituationMonitoring",
    meta: {
      title: "虫情模块",
    },
    component: () => import("@/views/HomeView.vue"),
    children: [
      {
        name: "index1",
        path: "/index1",
        meta: {
          title: "实时虫害",
        },
        component: () => import("@/views/insituationMonitoring/index1.vue"),
      },
      {
        name: "insectpestCharts",
        path: "/insectpestCharts",
        meta: {
          title: "虫情统计",
        },
        component: () =>
          import("@/views/insituationMonitoring/insectpestCharts.vue"),
      },
      {
        name: "pestAnalysis",
        path: "/pestAnalysis",
        meta: {
          title: "虫情分析",
        },
        component: () =>
          import("@/views/insituationMonitoring/pestAnalysis.vue"),
      },
    ],
  },
  {
    name: "meteorologicalManagement",
    path: "/meteorologicalManagement",
    meta: {
      title: "环境模块",
    },
    component: () => import("@/views/HomeView.vue"),
    children: [
      {
        name: "index",
        path: "/index",
        meta: {
          title: "环境虫害",
        },
        component: () => import("@/views/meteorologicalManagement/index.vue"),
      },
      {
        name: "meteorologicalChart",
        path: "/meteorologicalChart",
        meta: {
          title: "环境统计",
        },
        component: () =>
          import("@/views/meteorologicalManagement/meteorologicalChart.vue"),
      },
      {
        name: "soilmoisturedataChart",
        path: "/soilmoisturedataChart",
        meta: {
          title: "环境分析",
        },
        component: () =>
          import("@/views/meteorologicalManagement/soilmoisturedataChart.vue"),
      },
    ],
  },
  {
    name: "login",
    path: "/",
    meta: {
      title: "登录页",
    },
    component: () => import("@/components/login/login.vue"),
  },
  {
    name: "404",
    path: "/:pathMatch(.*)*", // 捕获所有路径的路由
    meta: {
      hidden: true,
    },
    component: () => import("@/components/error/error.vue"),
  },
];
// // 路由数组合并
// routes = [...routes];
// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// 路由守卫
router.afterEach((to) => {
  if (to.meta.title) {
    document.title = to.meta.title as string;
  }
});
export default router;
